// pages/uploader/uploader.js
const Q = require('../../resources/bce-bos-uploader-lite/vendor/q.js');
const StsTokenManager = require('../../resources/bce-bos-uploader-lite/sts_token_manager.js')
const Uploader = require('../../resources/bce-bos-uploader-lite/uploader.js');
const uploader = new Uploader({
  uptoken_url: 'http://172.17.150.42:8080/web/api/bos/ack.do',
  init: {
    FilesAdded: function (_, files) {
      console.log('FilesAdded', _, files);
    },
    FileUploaded: function (_, file, info) {
      console.log('FileUploaded', _, file, info);
    },
    UploadComplete: function () {
      console.log('UploadComplete');
    },
    Key: function (_, file) {
        var deferred = Q.defer();
        var stm = new StsTokenManager({
          uptoken_url: 'http://172.17.150.42:8080/web/api/bos/ack.do'
        });
        stm.get('zskd-pub').then(function(payload){
          uploader.setOptions({
              bos_ak: payload.AccessKeyId,
              bos_sk: payload.SecretAccessKey,
              uptoken: payload.SessionToken
        });
        var path = 'test/res';
        var name = file.name,
          type = file.type;
        var date = new Date(),
          year = date.getFullYear(),
          month = date.getMonth() + 1,
          day = date.getDate(),
          milliSeconds = date.getMilliseconds(),
          hour = date.getHours(),
          minute = date.getMinutes(),
          second = date.getSeconds();
        month = month < 10 ? '0' + month : month;
        day = day < 10 ? '0' + day : day;
        hour = hour < 10 ? '0' + hour : hour;
        minute = minute < 10 ? '0' + minute : minute;
        second = second < 10 ? '0' + second : second;
        milliSeconds = milliSeconds < 10 ? '00' + milliSeconds : milliSeconds < 100 ? '0' + milliSeconds : milliSeconds;
        var min = 10000,
          max = 99999;
        var random = parseInt(Math.random() * (max - min + 1)) + min;
        var ext = name.substring(name.lastIndexOf('.')),
          folder = 'file';
        if (/(image|audio|video)/.test(type)) {
          folder = type.replace(/^(image|audio|video)(.*)$/, '$1');
        }
        var key = [path, '/', folder, '/', year, month, day, hour, minute, second, milliSeconds, random, ext].join('');
        deferred.resolve({
          key: key
        });
      }).catch(function(){
          deferred.reject(new Error(''));
      });
      return deferred.promise;
    }

  }
});
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  bindUploader: function() {
    var that = this;
    wx.chooseImage({
      success: function(res) {
        var tempFiles = res.tempFiles;
        that.addFile(tempFiles);
      }
    });
  },

  addFile: function(tempFiles) {
    uploader.addFilesOuter(tempFiles);
  }

})